<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="吨位值" prop="mtValues">
        <el-input
          v-model="queryParams.mtValues"
          placeholder="请输入吨位值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:machineTons:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:machineTons:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:machineTons:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['base:machineTons:export']"
        >
          导出
        </el-button>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: 35px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="machineTonsList"
      border
      stripe
      highlight-current-row
      height="calc(100% - 100px)"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="吨位代码" align="center" prop="mtCode" />
      <el-table-column label="吨位值" align="center" prop="mtValues" />
      <el-table-column label="宽度MIN" align="center" prop="mtWidthmin" />
      <el-table-column label="宽度MAX" align="center" prop="mtWidthmax" />
      <el-table-column label="厚度MIN" align="center" prop="mtHeightmin" />
      <el-table-column label="厚度MAX" align="center" prop="mtHeightmax" />
      <el-table-column label="有效标识" align="center" key="mtFlag">
        <template #default="scope">
          <el-switch
            v-model="scope.row.mtFlag"
            active-value="0"
            inactive-value="1"
            @change="handleMacFlag(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="建立人员" align="center" prop="createBy" />
      <el-table-column
        label="建立时间"
        align="center"
        prop="createTime"
        width="150"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更改人员" align="center" prop="updateBy" />
      <el-table-column
        label="更改时间"
        align="center"
        prop="updateTime"
        width="150"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        width="150"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:machineTons:edit']"
          >
            修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:machineTons:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改机台吨位对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="550px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="吨位值" prop="mtValues">
          <el-input v-model="form.mtValues" placeholder="请输入吨位值" />
        </el-form-item>
        <el-form-item label="宽度MIN" prop="mtWidthmin">
          <el-input v-model="form.mtWidthmin" placeholder="请输入宽度MIN" />
        </el-form-item>
        <el-form-item label="宽度MAX" prop="mtWidthmax">
          <el-input v-model="form.mtWidthmax" placeholder="请输入宽度MAX" />
        </el-form-item>
        <el-form-item label="厚度MIN" prop="mtHeightmin">
          <el-input v-model="form.mtHeightmin" placeholder="请输入厚度MIN" />
        </el-form-item>
        <el-form-item label="厚度MAX" prop="mtHeightmax">
          <el-input v-model="form.mtHeightmax" placeholder="请输入厚度MAX" />
        </el-form-item>
        <el-form-item label="有效标识">
          <el-radio-group v-model="form.mtFlag">
            <el-radio
              v-for="dict in mtFlagOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改机台吨位对话框 -->
    <el-dialog
      :title="title"
      v-model="open1"
      :close-on-click-modal="false"
      draggable
      width="550px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="宽度MIN" prop="mtWidthmin">
          <el-input v-model="form.mtWidthmin" placeholder="请输入宽度MIN" />
        </el-form-item>
        <el-form-item label="宽度MAX" prop="mtWidthmax">
          <el-input v-model="form.mtWidthmax" placeholder="请输入宽度MAX" />
        </el-form-item>
        <el-form-item label="厚度MIN" prop="mtHeightmin">
          <el-input v-model="form.mtHeightmin" placeholder="请输入厚度MIN" />
        </el-form-item>
        <el-form-item label="厚度MAX" prop="mtHeightmax">
          <el-input v-model="form.mtHeightmax" placeholder="请输入厚度MAX" />
        </el-form-item>
        <el-form-item label="有效标识">
          <el-radio-group v-model="form.mtFlag">
            <el-radio
              v-for="dict in mtFlagOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMachineTons,
  getMachineTons,
  delMachineTons,
  addMachineTons,
  updateMachineTons,
  exportMachineTons,
  changeMacFlag,
} from "@/api/base/machineTons";

export default {
  name: "MachineTons",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 机台吨位表格数据
      machineTonsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 状态:0启用 1停用字典
      mtFlagOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 20 : 15,
        mtValues: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        mtValues: [
          { required: true, message: "吨位值不能为空", trigger: "blur" },
        ],
        mtWidthmin: [
          { required: true, message: "宽度MIN不能为空", trigger: "blur" },
        ],
        mtWidthmax: [
          { required: true, message: "宽度MAX不能为空", trigger: "blur" },
        ],
        mtHeightmin: [
          { required: true, message: "厚度MIN不能为空", trigger: "blur" },
        ],
        mtHeightmax: [
          { required: true, message: "厚度MAX不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_flag").then((response) => {
      this.mtFlagOptions = response.data;
    });
  },
  methods: {
    /** 查询机台吨位列表 */
    getList() {
      this.loading = true;
      listMachineTons(this.queryParams).then((response) => {
        this.machineTonsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open1 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        mtId: null,
        mtCode: null,
        mtValues: null,
        mtWidthmin: null,
        mtWidthmax: null,
        mtHeightmin: null,
        mtHeightmax: null,
        mtFlag: "0",
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.mtId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加机台吨位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const mtId = row.mtId || this.ids;
      getMachineTons(mtId).then((response) => {
        this.form = response.data;
        this.open1 = true;
        this.title = "修改机台吨位";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.mtId != null) {
            updateMachineTons(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open1 = false;
              this.getList();
              const flag = 1;
            });
          } else {
            addMachineTons(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleMacFlag(row) {
      let text = row.mtFlag === "0" ? "启用" : "停用";
      this.$confirm('确认要"' + text + "吗?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return changeMacFlag(row.mtId, row.mtFlag);
        })
        .then(() => {
          this.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.mtFlag = row.mtFlag === "0" ? "1" : "0";
        });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const mtIds = row.mtId || this.ids;
      this.$confirm(
        '是否确认删除机台吨位编号为"' + mtIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delMachineTons(mtIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有机台吨位数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportMachineTons(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
